import moment from '_moment@2.29.1@moment';
import { Space, Tag } from 'antd';
import ActionBuilder from './ActionBuilder'

const ColumnBuilder = (tableColumn: BasicListApi.Field[] | undefined, actionHandler: BasicListApi.ActionHandler) => {
  const newColumns: BasicListApi.Field[] = [];
  (tableColumn || []).forEach(column => {
    if(column.hideInColumn !== true) {
      switch (column.type) {
        case 'datetime':

          column.render =  (value: any) => {
            return moment(value).format('YYYY-MM-DD HH:mm:ss')
          } 
        break;
        case 'switch':
          column.render = (value: any) => {
            const option = (column.data || []).find((item: any) =>item.value === value)
            return <Tag color={value ? 'blue' : 'red'}>{option?.title}</Tag>
          }
        break;
        case 'actions':
          column.render = (_: any, record: any) => {
            return <Space>{ActionBuilder(column.actions, actionHandler, false, record)}</Space>
          }
        break;

        default:
          break;
      }
      newColumns.push(column)
    }

  })
//  [{title: 'ID', dataIndex: 'id', key: 'id'}].concat(init?.data?.layout?.tableColumn.filter(item =>item.hideInColumn !== true) || [])
  const idColumns = [{title: 'ID', dataIndex: 'id', key: 'id'}]
 return idColumns.concat(newColumns);
}
export default ColumnBuilder
